package com.animation;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ScreenUtils;
import com.esotericsoftware.spine.AnimationState;
import com.esotericsoftware.spine.AnimationStateData;
import com.esotericsoftware.spine.Skeleton;
import com.esotericsoftware.spine.SkeletonData;
import com.esotericsoftware.spine.SkeletonJson;
import com.esotericsoftware.spine.SkeletonRenderer;
import com.esotericsoftware.spine.SkeletonRendererDebug;

public class Animation extends ApplicationAdapter {
	OrthographicCamera camera;
	PolygonSpriteBatch batch;
	SkeletonRenderer renderer;
	SkeletonRendererDebug debugRenderer;
	TextureAtlas atlas;
	Skeleton skeleton;
	AnimationState state;
	public void create () {
		camera = new OrthographicCamera();
		batch = new PolygonSpriteBatch();
		renderer = new SkeletonRenderer();
		renderer.setPremultipliedAlpha(true);
		debugRenderer = new SkeletonRendererDebug();
		debugRenderer.setMeshTriangles(false);
		debugRenderer.setRegionAttachments(false);
		debugRenderer.setMeshHull(false);

		atlas = new TextureAtlas(Gdx.files.internal("axie/axie.atlas"));
		SkeletonJson json = new SkeletonJson(atlas);
		json.setScale(0.6f);
		SkeletonData skeletonData = json.readSkeletonData(Gdx.files.internal("axie/axie.json"));
		skeleton = new Skeleton(skeletonData);
		skeleton.setPosition(0,0);
		AnimationStateData stateData = new AnimationStateData(skeletonData);
		state = new AnimationState(stateData);
		state.setAnimation(0, "last-stand/attack/melee/toothless-fade-in", true);
		state.addAnimation(1, "action/rise-to-entrance", true, 2);
	}
	public void render () {
		state.update(Gdx.graphics.getDeltaTime());
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		if (state.apply(skeleton)) skeleton.updateWorldTransform();//显示图片
		camera.update();

//		Vector2 v2=new Vector2();
//		v2.x=skeleton.getX();
//		v2.y=skeleton.getY();
//		camera.translate(v2);

		batch.begin();
		renderer.draw(batch, skeleton);
		batch.end();


		debugRenderer.draw(skeleton);
	}
	public void resize (int width, int height) {
		camera.setToOrtho(false);
	}
	public void dispose () {
		atlas.dispose();
	}
}
